import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);


import { getuserInfo, getCarInfo } from "../api/index.js";

export default new Vuex.Store({
    state: {
        userinfo: {},
        carList: [],
        badgeCount: 0,
        allChecked: false,
        token: sessionStorage.getItem('token') ? sessionStorage.getItem('token') : '',
        isLogin: sessionStorage.getItem('isLogin') ? sessionStorage.getItem('isLogin') : false
    },
    getters: {
        getIsLogin(state) {
            return state.isLogin
        },
        getToken(state) {
            return state.token
        },
        getUser(state) {
            return state.userinfo
        },
        getCar(state) {
            return state.carList
        },
        getBadgeCount(state) {
            return state.badgeCount
        },
        getallChecked(state) {
            return state.allChecked
        },
        getAllPrice(state) {
            let price = 0
            state.carList.forEach(ele => {
                if (ele.checked) {
                    price += ele.count * ele.price
                }
            })
            return price
        }
    },
    mutations: {
        setToken(state, data) {
            if (data) {
                sessionStorage.setItem('token', data.token)
                sessionStorage.setItem('isLogin', true)
                state.token = data.token
                state.isLogin = true
            } else {
                // console.log();
                state.isLogin = false
            }

        },
        saveUserInfo(state, data) {
            sessionStorage.setItem('userinfo', JSON.stringify(data))
            state.userinfo = data
        },
        saveCarInfo(state, data) {
            if (data) {
                data.forEach(ele => {
                    if (state.carList !== []) {
                        state.carList.forEach(item => ele.checked = item.checked)
                    } else {
                        ele.checked = false
                    }
                });
                console.log(data);
                state.badgeCount = data.map(el => el.count).reduce((pre, cur) => pre + cur)
            } else {
                // console.log("-------");
                state.badgeCount = ""
            }
            sessionStorage.setItem("carinfo", data)
                // console.log(data);
            state.carList = data
        },
        setAllChecked(state, params) {
            state.allChecked = params
        },
    },
    actions: {
        getInfo(state) {
            getuserInfo(res => {
                state.commit('saveUserInfo', res.data)
            })
        },
        getcars(state) {
            getCarInfo(res => {
                state.commit('saveCarInfo', res.data)
            })
        },
        allcheckChange(state) {
            state.getters.getCar.forEach(ele => {
                ele.checked = !state.getters.getallChecked
            })
            state.commit("setAllChecked", !state.getters.getallChecked)
        },
        singleChange(state) {
            let flag = true
            state.getters.getCar.forEach(ele => {
                if (!ele.checked) {
                    flag = false
                }
            });
            state.commit("setAllChecked", flag)
        }

    },
});